package com.ruoyi.web.controller.system;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.core.page.ResultEntity;
import com.ruoyi.system.domain.TProd;
import com.ruoyi.system.domain.TProdType;
import com.ruoyi.system.service.TProdTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.ValidationException;
import java.util.List;

/**
 * @author liangjiuqing
 * @since 2024-09-25 15:04
 */
@Api(tags = "产品类型接口")
@RestController
@RequestMapping("/prodType")
public class TProdTypeController {
    @Autowired
    private TProdTypeService prodTypeService;

    @ApiOperation(value = "查询产品类型列表")
    @GetMapping("/listProdTypes")
    public ResultEntity<List<TProdType>> listProdTypes() {
        return ResultEntity.createSuccess(prodTypeService.list());
    }

    @ApiOperation(value = "新增或修改产品类型")
    @PostMapping("/saveOrUpdateTProdType")
    public ResultEntity<Void> saveOrUpdateTProdType(@Validated @RequestBody TProdType prodType) {
        if (prodTypeService.count(Wrappers.<TProdType>lambdaQuery()
                .eq(TProdType::getCode, prodType.getCode())
                .ne(prodType.getId() != null, TProdType::getId, prodType.getId())) > 0) {
            throw new ValidationException("code已存在");
        }
        prodTypeService.saveOrUpdate(prodType);
        return ResultEntity.createSuccess(null);
    }

    @ApiOperation(value = "批量删除产品类型")
    @GetMapping("/removeProdTypeByIds/{ids}")
    public ResultEntity<Void> removeProdTypeByIds(@ApiParam(value = "主键ID，逗号拼接", required = true) @PathVariable List<Long> ids) {
        prodTypeService.removeByIds(ids);
        return ResultEntity.createSuccess(null);
    }
}
